package com.projectbox.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import com.projectbox.dao.UserDao;
import com.projectbox.model.User;
import com.projectbox.service.UserService;
import com.projectbox.utility.EmailUtility;

public class UserServiceImpl implements UserService {

	private final Log logger = LogFactory.getLog(getClass());
	
	private UserDao userDao;
	private PasswordEncoder passwordEncoder;
	private EmailUtility emailUtility;
		
	public void setEmailUtility(EmailUtility emailUtility) {
		this.emailUtility = emailUtility;
	}
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
		this.passwordEncoder = passwordEncoder;
	}

	public User getCurrentUser() {
		// TODO Auto-generated method stub
		String userName = null;

		Object principal = SecurityContextHolder.getContext()
				.getAuthentication().getPrincipal();

		if (principal instanceof UserDetails) {
			userName = ((UserDetails) principal).getUsername();
		} else {
			return null;
		}

		return userDao.getUserByUserName(userName);
	}

	public List<User> findAllUsers() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean registerUser(String username, String password,
			String firstName, String lastName) {

		logger.debug("registeUser for " + username);

		User user = new User();
		user.setUsername(username);
		user.setFirstName(firstName);
		user.setLastName(lastName);
		String encodePass = this.passwordEncoder.encodePassword(password, null);
		user.setPassword(encodePass);
		user.setEnabled(true);

		boolean success = this.userDao.insert(user);
		if (success && user.getId() != null) {
			this.emailUtility
					.sendMail(
							"admin@projectbox.com",
							user.getUsername(),
							"Welcome to ProjectBox",
							"Welcome to ProjectBox! \nThanks for registering with our service. You can now login with your e-mail and password and start collaborating. "
									+ "Dont forget to check out the Announcment Page for new updates and the Rules page! \nProjectBox Team");
			return true;
		}
		return false;
	}

	public void updateUserInfo(Long id, String username, String password,
			String firstName, String lastName) {
		// TODO Auto-generated method stub

	}

	public void enableUser(Long id) {
		this.userDao.setEnabled(id, true);
	}
	
	public void disableUser(Long id) {
		this.userDao.setEnabled(id, false);
	}

	public User findUser(Long id) {
		return this.userDao.getUserById(id);
	}
	
	public User findUser(String email) {
		return this.userDao.getUserByUserName(email);
	}

	public boolean isAdmin(String username) {
		logger.debug("check for admin");
		
		List<String> authorities = this.userDao.getAuthoritiesByUsername(username);
		
		for (String authority : authorities) {
			if (authority.equals("ROLE_ADMIN")) {
				return true;
			}
		}
		
		return false;
	}

}
